﻿using System.Security.Principal;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System;

namespace GBlog.Core.Entities
{
    public class User : EntityBase<int>, IPrincipal
    {
        public void CreateAuth()
        {
            this.Identity = new GenericIdentity(Username);
        }

        public string Username { get; set; }
        [ScriptIgnore]
        public string Password { get; set; }
        public string Email { get; set; }
        public string Seo { get; set; }
        public string Website { get; set; }
        public Access Access { get; set; }

        [ScriptIgnore]
        public object Tag { get; set; }
        [ScriptIgnore]
        public IIdentity Identity { get; private set; }

        public virtual ICollection<Post> Posts { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }

        public bool IsInRole(string role)
        {
            Access access;
            if (Enum.TryParse<Access>(role, out access))
            {
                Access useraccess = (Access)this.Access;
                if (useraccess.HasFlag(access))
                {
                    return true;
                }
            }
            return false;
        }
    }
}